<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('学生端申请顶岗实习填报信息')"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-edit" th:object="${work}">
        <input id="workId" name="workId" th:field="*{workId}" type="hidden"/>
        <input id="workBegin" name="workBegin" th:field="*{workBegin}" type="hidden"/>
        <input id="workEnd" name="workEnd" th:field="*{workEnd}" type="hidden"/>
        <div class="form-group">
            <label class="col-sm-3 control-label ">名称：</label>
            <div class="col-sm-8">
                <input class="form-control" id="productionName" name="productionName" readonly th:field="*{workName}"
                       type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">实习企业：</label>
            <div class="col-sm-8">
                <input class="form-control" id="company" name="company" required type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">实习地点：</label>
            <div class="col-sm-8">
                <input class="form-control" id="site" name="site" required type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">开始时间：</label>
            <div class="col-sm-8">
                <input class="form-control" id="begining" name="begining" required type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">结束时间：</label>
            <div class="col-sm-8">
                <input class="form-control" id="ending" name="ending" required type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">实习内容：</label>
            <div class="col-sm-8">
                <textarea class="form-control" id="content" name="content" required type="text"></textarea>
            </div>
        </div>
        <div class="form-group" id="clid" name="clid">
            <div>
                <label class="col-sm-3 control-label ">置换课程：</label>
                <div class="col-sm-8">
                    <input class="form-control" id="courseName" name="courseName" type="text">
                    <input onclick="del(this);" type="button" value="删除">
                    <input onclick="add();" type="button" value="添加">
                </div>
            </div>
        </div>

    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: ztree-js"/>
<th:block th:include="include :: datetimepicker-js"/>
<script type="text/javascript">

    $(function () {
        $("#begining").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        $("#ending").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    });

    function add() {
        //获取要克隆的对象
        var cloneDiv = $("#clid div").eq(0);
        //克隆
        var newDiv = cloneDiv.clone();
        //添加
        //添加前清空值
        $("input[type='text']", newDiv).val("");
        //添加
        newDiv.appendTo("#clid");
    }

    //删除
    function del(btn) {
        //获取要删除的对象
        var delDiv = $(btn).parent().parent();
        //当最后一个的时候不可删除
        var lastDiv = $("#clid div");
        if (lastDiv.length > 2) {
            delDiv.remove();
        }
    }


    function edit() {
        var workId = $("input[name='workId']").val();
        var company = $("input[name='company']").val();
        var site = $("input[name='site']").val();
        var begining = $("input[name='begining']").val();
        var ending = $("input[name='ending']").val();
        var content = $("textarea[name='content']").val();
        var workBegin = $("input[name='workBegin']").val();
        var workEnd = $("input[name='workEnd']").val();
        var courses = [];
        $("#clid div div #courseName").each(function (i) {
            courses[i] = $(this).val()
        });
        $.ajax({
            cache: true,
            type: "POST",
            url: ctx + "sau/workapply/addWorkApply",
            data: {
                "workId": workId,
                "company": company,
                "site": site,
                "begining": begining,
                "ending": ending,
                "content": content,
                "workBegin": workBegin,
                "workEnd": workEnd,
                "courses": courses.join(',')
            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.successCallback(data);
            }
        });
    }

    function submitHandler() {
        if ($.validate.form()) {
            edit();
        }
    }
</script>
</body>
</html>
